package com.baibei.widget.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.InsetDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.baibei.widget.PaymentEditText;
import com.baibei.widget.R;
import com.rae.widget.dialog.impl.AppDialog;

import java.text.DecimalFormat;

/**
 * 支付密码输入框
 * Created by ChenRui on 2017/5/15 0015 10:50.
 */
public class PayPasswordDialog extends AppDialog {

    TextView mTitleView;
    TextView mHintTitleView;
    TextView mMoneyView;
    PaymentEditText mPaymentView;
    private DecimalFormat mDecimalFormat;

    private DialogInterface.OnDismissListener mOnSuccessDismissListener;

    public PayPasswordDialog(Context context) {
        // 应用主题，解决键盘顶起的时候内容被顶的问题
        super(context, R.style.Theme_AppCompat_Dialog_Alert);
    }

    @Override
    protected void initDialog() {
        super.initDialog();
        if (getWindow() != null) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
        setContentView(R.layout.widget_dialog_pay_password_input);
        if (getWindow() != null) {
            initWindowAttr(getWindow());
        }
        mDecimalFormat = new DecimalFormat();
        mDecimalFormat.applyPattern(",###,##0.00");
        mTitleView = (TextView) findViewById(R.id.tv_title);
        mHintTitleView = (TextView) findViewById(R.id.tv_hint_title);
        mMoneyView = (TextView) findViewById(R.id.tv_money);
        mPaymentView = (PaymentEditText) findViewById(R.id.et_payment_password);
        findViewById(android.R.id.closeButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        setCanceledOnTouchOutside(false);
        mPaymentView.setOnTextFinishListener(new PaymentEditText.OnTextFinishListener() {
            @Override
            public void onTextFinish(CharSequence text, int length) {
                if (mOnSuccessDismissListener != null) {
                    mOnSuccessDismissListener.onDismiss(PayPasswordDialog.this);
                }
                dismiss();
            }
        });

        getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > 0)) {
                    //软键盘弹起 doSomethinh..
                    Log.e("rae", "软键盘弹起");
                } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > 0)) {
                    //软件盘关闭 doSomethinh..
                    Log.e("rae", "软件盘关闭");
                }
            }
        });

    }

    private void initWindowAttr(Window window) {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        Drawable drawable = new InsetDrawable(new ColorDrawable(Color.TRANSPARENT), 48, 0, 48, 0);
        window.setBackgroundDrawable(drawable);
        window.getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
        window.getAttributes().height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.getAttributes().gravity = Gravity.LEFT | Gravity.RIGHT | Gravity.START | Gravity.END;
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleView.setText(title);
    }

    @Override
    public void setMessage(CharSequence message) {
        mHintTitleView.setText(message);
    }

    public void setHintText(String text) {
        mHintTitleView.setText(text);
    }

    public void setMoney(double money) {
        String text = "¥" + mDecimalFormat.format(money);
        mMoneyView.setText(text);
    }

    @Override
    public void show() {
        super.show();
        mPaymentView.setText("");
    }

    /**
     * 校验成功监听
     */
    public void setOnSuccessDismissListener(OnDismissListener onSuccessDismissListener) {
        mOnSuccessDismissListener = onSuccessDismissListener;
    }

    /**
     * 获取输入的密码
     */
    public String getPassword() {
        return mPaymentView.getText().toString();
    }

}
